using System.Collections.Generic;

namespace CheckoutCodeKata
{
    public class SpecialPricesRule : IPriceRule
    {
        private readonly IPriceRule _nextPriceRule;
        private readonly Counter _counter;
        private readonly List<SpecialPriceRule> _specialPriceRules;

        public SpecialPricesRule(IPriceRule nextPriceRule, Counter counter, List<SpecialPriceRule> specialPriceRules)
        {
            _nextPriceRule = nextPriceRule;
            _counter = counter;
            _specialPriceRules = specialPriceRules;
        }

        public void CalculatePrice(string code, int quantity)
        {
            int remaining = quantity;
            _specialPriceRules.ForEach( rule => remaining = rule.ApplyPriceRule(remaining, code, _counter));
            _nextPriceRule.CalculatePrice(code, remaining);
        }
    }
}